db.test.php

<?php
include("db.conn.php");
include("db.config.php");

 

$config = new Config_Conn();
$mssql = new mDataBase($config);
$Sql = "select id,username,password from test";
$result = $mssql->Query($Sql);
while($rs = $mssql->FetchArray($result))
 echo $rs["id"] ." ". $rs["username"] ."". $rs["password"]  ."<br>";
?>

 

db.config.php

<?php
class Config_Conn
{
 var $Server = "127.0.0.1";
 var $UserName = "sa";
 var $Password = "sa";
 var $Port = "1433";
 var $dbName = "data";
 var $conType = "c";
 var $dbType = "mssql";
}

?>

 

db.conn.php

<?PHP

Class mDataBase
{
 
 var $dbType;  // 数据库类型: mssql, mysql, pg
 var $Server;  // 连接服务器地址
 var $dbName;  // 数据库名称
 var $UserName;  // 数据库登录名
 var $Password;  // 数据库密码
 var $conType;  // 连接类型: c - common connection, p - open persistent connection
 var $idCon;  // 连接号
 var $curRow;  // current row number of data from the result
    // associated with the specified result identifier array
 var $seek;  // current row number of data from DataSeek function array

 
 function mDataBase($config)
 {
  $this->Server = $config->Server;
  $this->dbName = $config->dbName;
  $this->UserName = $config->UserName;
  $this->Password = $config->Password;
  $this->dbType = $config->dbType;
  $this->conType = $config->conType;
  switch($this->dbType)
  {
   case "mssql":
    if($this->conType == "c")
     $idCon = mssql_connect($this->Server, $this->UserName, $this->Password);
    else
     $idCon = mssql_pconnect($this->Server, $this->UserName, $this->Password);
    break;
   case "mysql":
    if($this->conType == "c")
    {
     $idCon = mysql_connect($this->Server, $this->UserName, $this->Password);
     mysql_query("SET NAMES 'GBK'");
    }else{
     $idCon = mysql_pconnect($this->Server, $this->UserName, $this->Password);
     mysql_query("SET NAMES 'GBK'");
    }
    break;
   case "pg":
    if($this->conType == "c")
     $idCon = pg_connect($this->Server . " user=" . $this->UserName . " password=" . $this->Password);
    else
     $idCon = pg_pconnect($this->Server . " user=" . $this->UserName . " password=" . $this->Password);
    break;
   default:
    $idCon = 0;
    break;
  }
  $this->idCon = $idCon;
  if($this->dbName != "")
   $this->SelectDB($this->dbName);
  return $idCon;
 }

 
 function SelectDB($dbName)
 {
  switch($this->dbType)
  {
   case "mssql":
    mssql_select_db($dbName, $this->idCon);
    break;
   case "mysql":
    mysql_select_db($dbName, $this->idCon);
    break;
   case "pg":
    break;
   default:
    break;
  }
 }

 
 function Close()
 {
  switch($this->dbType)
  {
   case "mssql":
    mssql_close($this->idCon);
    break;
   case "mysql":
    mysql_close($this->idCon);
    break;
   case "pg":
    pg_close($this->idCon);
    break;
  }
 }

 
 function Query($query)
 {
  switch($this->dbType)
  {
   case "mssql":
    $r = mssql_query($query, $this->idCon);
    break;
   case "mysql":
    $r = mysql_query($query, $this->idCon);
    break;
   case "pg":
    $r = pg_exec($this->idCon, $query);
    break;
   default:
    $r = false;
    break;
  }
  $this->curRow[$r] = 0;
  $this->seek[$r]  = 0;
  return $r;
 }

 
 function DataSeek($result, $RowNumber)
 {
  switch($this->dbType)
  {
   case "mssql":
    $r = mssql_data_seek($result, $RowNumber);
    break;
   case "mysql":
    $r = mysql_data_seek($result, $RowNumber);
    break;
   case "pg":
    $r = false;
    break;
   default:
    $r = false;
    break;
  }
  $this->seek[$result] = (int)$RowNumber;
  return $r;
 }

 
 function FieldName($result, $offset)
 {
  switch($this->dbType)
  {
   case "mssql":
    $r = mssql_field_name($result, $offset);
    break;
   case "mysql":
    $r = mysql_field_name($result, $offset);
    break;
   case "pg":
    $r = pg_fieldname($result, $offset);
    break;
   default:
    $r = false;
    break;
  }
  return $r;
 }

 
 function FieldType($result, $offset)
 {
  switch($this->dbType)
  {
   case "mssql":
    $r = mssql_field_type($result, $offset);
    break;
   case "mysql":
    $r = mysql_field_type($result, $offset);
    break;
   case "pg":
    $r = pg_fieldtype($result, $offset);
    break;
   default:
    $r = false;
    break;
  }
  return $r;
 }

 
 function FieldLength($result, $offset)
 {
  switch($this->dbType)
  {
   case "mssql":
    $r = mssql_field_length($result, $offset);
    break;
   case "mysql":
    $r = mysql_field_len($result, $offset);
    break;
   case "pg":
    $r = pg_fieldsize($result, $offset);
    break;
   default:
    $r = false;
    break;
  }
  return $r;
 }

 
 function FetchRow($result, $RowNumber = 0)
 {
  switch($this->dbType)
  {
   case "mssql":
    $r = mssql_fetch_row($result);
    break;
   case "mysql":
    $r = mysql_fetch_row($result);
    break;
   case "pg":
    $r = pg_fetch_row($result, $RowNumber);
    if($r)
    {
     $this->curRow[$result] = $RowNumber;
     $this->seek[$result] = $RowNumber;
    }
    break;
   default:
    $r = false;
    break;
  }
  return $r;
 }

 
 function FetchArray($result, $RowNumber = 0, $ResultType = 2)
 {
  switch($this->dbType)
  {
   case "mssql":
    $r = mssql_fetch_array($result);
    break;
   case "mysql":
    $r = mysql_fetch_array($result);
    break;
   case "pg":
    $r = pg_fetch_array($result, $RowNumber, $ResultType);
    if($r)
    {
     $this->curRow[$result] = $RowNumber;
     $this->seek[$result] = $RowNumber;
    }
    break;
   default:
    $r = false;
    break;
  }
  return $r;
 }

 
 function FetchObject($result, $RowNumber = 0, $ResultType = 2)
 {
  switch($this->dbType)
  {
   case "mssql":
    $r = mssql_fetch_object($result);
    break;
   case "mysql":
    $r = mysql_fetch_object($result);
    break;
   case "pg":
    $r = pg_fetch_object($result, $RowNumber, $ResultType);
    if($r)
    {
     $this->curRow[$result] = $RowNumber;
     $this->seek[$result] = $RowNumber;
    }
    break;
   default:
    $r = false;
    break;
  }
  return $r;
 }

 
 function Result($result, $RowNumber, $FieldName)
 {
  switch($this->dbType)
  {
   case "mssql":
    $r = mssql_result($result, $RowNumber, $FieldName);
    break;
   case "mysql":
    $r = mysql_result($result, $RowNumber, $FieldName);
    break;
   case "pg":
    $r = pg_result($result, $RowNumber, $FieldName);
    break;
   default:
    $r = false;
    break;
  }
  return $r;
 }

 
 function FreeResult($result)
 {
  switch($this->dbType)
  {
   case "mssql":
    $r = mssql_free_result($result);
    break;
   case "mysql":
    $r = mysql_free_result($result);
    break;
   case "pg":
    $r = pg_freeresult($result);
    break;
   default:
    $r = false;
    break;
  }
  return $r;
 }

 
 function RowsNumber($result)
 {
  switch($this->dbType)
  {
   case "mssql":
    $r = mssql_num_rows($result);
    break;
   case "mysql":
    $r = mysql_num_rows($result);
    break;
   case "pg":
    $r = pg_numrows($result);
    break;
   default:
    $r = false;
    break;
  }
  return $r;
 }

 
 function FieldsNumber($result)
 {
  switch($this->dbType)
  {
   case "mssql":
    $r = mssql_num_fields($result);
    break;
   case "mysql":
    $r = mysql_num_fields($result);
    break;
   case "pg":
    $r = pg_numfields($result);
    break;
   default:
    $r = false;
    break;
  }
  return $r;
 }

 
 function CurRecNumber($result)
 {
  return $this->curRow[$result];
 }

 
 function RecordNumber($result)
 {
  return $this->CurRecNumber($result) + 1;
 }

 
 function MoveFirstRec($result)
 {
  switch($this->dbType)
  {
   case "pg":
    $r = $this->FetchRow($result, 0);
    break;
   default:
    $rn = $this->DataSeek($result, 0);
    if($rn)
    {
     $r = $this->FetchRow($result);
     if($r)
      $this->curRow[$result] = $this->seek[$result];
    }else{
     $r = false;
    }
    break;
  }
  return $r;
 }

 
 function MoveLastRec($result)
 {
  $rs = $this->RowsNumber($result);
  if($rs)
  {
   $rs--;
   switch($this->dbType)
   {
    case "pg":
     $r = $this->FetchRow($result, $rs);
     break;
    default:
     $rn = $this->DataSeek($result, $rs);
     if($rn)
     {
      $r = $this->FetchRow($result);
      if($r)
       $this->curRow[$result] = $this->seek[$result];
     }else{
      $r = false;
     }
     break;
   }
  }
  return $r;
 }

 
 function MovePreviousRec($result)
 {
  $rs = $this->CurRecNumber($result);
  if($rs)
  {
   $rs--;
   switch($this->dbType)
   {
    case "pg":
     $r = $this->FetchRow($result, $rs);
     break;
    default:
     $rn = $this->DataSeek($result, $rs);
     if($rn)
     {
      $r = $this->FetchRow($result);
      if($r)
       $this->curRow[$result] = $this->seek[$result];
     }else{
      $r = false;
     }
     break;
   }
  }
  return $r;
 }

 
 function MoveNextRec($result)
 {
  $rs = $this->CurRecNumber($result);
  $rn = $this->RowsNumber($result);
  $rs++;
  if($rs != $rn)
  {
   switch($this->dbType)
   {
    case "pg":
     $r = $this->FetchRow($result, $rs);
     break;
    default:
     $re = $this->FetchRow($result);
     if($re)
     {
      $r = $re;
      $this->curRow[$result]++;
      $this->seek[$result] = $this->curRow[$result];
     }else{
      $r = false;
     }
     break;
   }
  }
  return $r;
 }

 
 function MoveToRec($result, $RowNumber)
 {
  $rn = $this->RowsNumber($result);
  if($RowNumber > 0 && $RowNumber < $rn)
  {
   $RowNumber--;
   switch($this->dbType)
   {
    case "pg":
     $r = $this->FetchRow($result, $RowNumber);
     break;
    default:
     $rn = $this->DataSeek($result, $RowNumber);
     if($rn)
     {
      $r = $this->FetchRow($result);
      if($r)
       $this->curRow[$result] = $this->seek[$result];
     }else{
      $r = false;
     }
     break;
   }
  }
  return $r;
 }
}
?>
